<%@ page import="java.util.ArrayList" %>
<%@ page import="bean.Dep" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title>学生信息田添加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="./css/x-admin.css" media="all">
</head>

<body>
<%
    ArrayList<Dep> depList = (ArrayList<Dep>) request.getAttribute("depList");
%>
<div class="x-body">
    <form class="layui-form" action="/student_add" method="post">
        <div class="layui-form-item">
            <label for="sno" class="layui-form-label">
                <span class="x-red">*</span>学号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="sno" name="sno" required="" lay-verify="check_sno"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="sname" class="layui-form-label">
                <span class="x-red">*</span>姓名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="sname" name="sname" required="" lay-verify="check_name"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="sex" class="layui-form-label">
                <span class="x-red"> </span>性别
            </label>
            <div class="layui-input-inline">
                <label>
                    <select id="sex" class="sex" name="sex" lay-verify="check_sex">
                        <option value="">请选择学生性别</option>
                        <option value="男">男</option>
                        <option value="女">女</option>
                    </select>
                </label>
            </div>

        </div>
        <div class="layui-form-item">
            <label for="birthday" class="layui-form-label">
                <span class="x-red"> </span>生日
            </label>
            <%
                Date date = new Date(System.currentTimeMillis());
                String today = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date).substring(0, 10);
            %>
            <div id="birthday" class="layui-input-inline">
                <label>
                    <input type="date" min="1960-01-01" max="2099-12-31" name="birthday" class="layui-input">
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="dep" class="layui-form-label">
                <span class="x-red"> </span>院系
            </label>
            <div id="dep" class="layui-input-inline">
                <label>
                    <select name="dep">
                        <option value="">请选择学生院系</option>
                        <c:forEach var="dep" items="depList">
                            <%for (Dep dep : depList) {%>
                            <option value="<%=dep.getDno()%>"><%=dep.getDname()%>
                            </option>
                            <%}%>
                        </c:forEach>
                    </select>
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="add" class="layui-form-label">
            </label>
            <button id="add" class="layui-btn" lay-filter="add" lay-submit="" type="submit">
                修改
            </button>
        </div>

    </form>
</div>
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script src="./js/x-layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form()
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            check_sno: function (value) {
                if (value.length < 1) {
                    return '学号不能为空';
                }
                if (value.length !== 6 || !/^[0-9]*$/.test(value)) {
                    return '学号必须为6位数字';
                }
            },
            check_name: function (value) {
                if (value.length === 0) {
                    return '姓名不能为空';
                }
                if (value.length < 2) {
                    return '姓名至少得2个字符';
                }
            },
            check_sex: function (value) {
                console.log(value)
                // if (value!=='男'||value!=='女'){
                //     return "请选择性别"
                // }
            }
        });

        //监听提交
        form.on('submit(alter)', function (data) {
            layer.confirm('确定添加吗？', {icon: 4, title: '提示信息'}, function (index) {
                $.ajax({
                    url: "/student_add",
                    type: "post",
                    data: data.field,
                    success: function (data) {
                        layer.alert("添加成功", {icon: 7}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前frame
                            parent.location.reload();//刷新父页面
                        });
                    }
                });
            });
            return false;
        });
    });
</script>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>

</html>